package rexsee.core.browser.clazz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import rexsee.core.browser.Browser;

public class JavascriptInterfaceList {

	private final ArrayList<JavascriptInterface> list;
	private final HashMap<String, JavascriptInterface> creations;
	public final Browser mBrowser;
	public JavascriptInterfaceList(Browser browser) {
		list = new ArrayList<JavascriptInterface>();
		creations = new HashMap<String, JavascriptInterface>();
		mBrowser = browser;
	}
	public ArrayList<JavascriptInterface> getList() {
		return list;
	}
	public String get() {
		String rtn = "";
		for (int i = 0; i < list.size(); i++) {
			if (i > 0) rtn += ",";
			rtn += "\"" + list.get(i).getInterfaceName() + "\"";
		}
		return "[" + rtn + "]";
	}
	public JavascriptInterface get(String interfaceName) {
		for (int i = 0; i < list.size(); i++) {
			JavascriptInterface tmp = list.get(i);
			if (tmp.getInterfaceName().equals(interfaceName)) return tmp;
		}
		return null;
	}
	public void add(JavascriptInterface javascriptInterface) {
		list.remove(javascriptInterface);
		list.add(javascriptInterface);
		mBrowser.addJavascriptInterface(javascriptInterface, javascriptInterface.getInterfaceName());
	}
	public void remove(String interfaceName) {
		JavascriptInterface face = get(interfaceName);
		if (face == null) return;
		mBrowser.addJavascriptInterface(null, interfaceName);
		list.remove(face);
	}
	public void inherit() {
		if (mBrowser.parentBrowser == null) return;
		for (int i = 0; i < mBrowser.parentBrowser.interfaceList.list.size(); i++) {
			JavascriptInterface tmp = mBrowser.parentBrowser.interfaceList.list.get(i).getInheritInterface(mBrowser);
			if (tmp != null) this.add(tmp);
		}
		mBrowser.addJavascriptInterface(mBrowser.parentBrowser.interfaceList.get(mBrowser.function.getInterfaceName()), mBrowser.function.getParentInterfaceName());
	}
	public boolean contains(String interfaceName) {
		JavascriptInterface face = get(interfaceName);
		return (face == null) ? false : true;
	}
	public int size() {
		return list.size();
	}
	public boolean isInherit(String interfaceName) {
		if (mBrowser.parentBrowser == null) return false;
		JavascriptInterface me = get(interfaceName);
		JavascriptInterface parent = mBrowser.parentBrowser.interfaceList.get(interfaceName);
		return (me != null && me.equals(parent)) ? true : false;
	}

	public boolean createFromParent(String interfaceName, String shortcut) {
		if (creations.containsKey(shortcut)) {
			mBrowser.exception(this.getClass().getSimpleName(), "Duplicated shortcut name.");
			return false;
		}
		if (contains(shortcut)) {
			mBrowser.exception(this.getClass().getSimpleName(), "Shortcut is duplicated in original interface list.");
			return false;
		}
		if (mBrowser.parentBrowser == null) {
			mBrowser.exception(this.getClass().getSimpleName(), "Parent browser is not available.");
			return false;
		}
		JavascriptInterface face = mBrowser.parentBrowser.interfaceList.get(interfaceName);
		if (face == null) {
			mBrowser.exception(this.getClass().getSimpleName(), "Interface does not exist in parent.");
			return false;
		}
		JavascriptInterface newFace = face.getNewInterface(mBrowser);
		mBrowser.addJavascriptInterface(newFace, shortcut);
		creations.put(shortcut, newFace);
		return true;
	}
	public boolean createShortcutFromParent(String interfaceName, String shortcut) {
		if (creations.containsKey(shortcut)) {
			mBrowser.exception(this.getClass().getSimpleName(), "Duplicated shortcut name.");
			return false;
		}
		if (contains(shortcut)) {
			mBrowser.exception(this.getClass().getSimpleName(), "Shortcut is duplicated in original interface list.");
			return false;
		}
		if (mBrowser.parentBrowser == null) {
			mBrowser.exception(this.getClass().getSimpleName(), "Parent browser is not available.");
			return false;
		}
		JavascriptInterface face = mBrowser.parentBrowser.interfaceList.get(interfaceName);
		if (face == null) {
			mBrowser.exception(this.getClass().getSimpleName(), "Interface does not exist in parent.");
			return false;
		}
		mBrowser.addJavascriptInterface(face, shortcut);
		creations.put(shortcut, face);
		return true;
	}
	public boolean create(String interfaceName, String shortcut) {
		if (creations.containsKey(shortcut)) {
			mBrowser.exception(this.getClass().getSimpleName(), "Duplicated shortcut name.");
			return false;
		}
		if (contains(shortcut)) {
			mBrowser.exception(this.getClass().getSimpleName(), "Shortcut is duplicated in original interface list.");
			return false;
		}
		JavascriptInterface face = get(interfaceName);
		if (face == null) {
			mBrowser.exception(this.getClass().getSimpleName(), "Interface does not exist.");
			return false;
		}
		JavascriptInterface newFace = face.getNewInterface(mBrowser);
		if (newFace.equals(face)) {
			mBrowser.addJavascriptInterface(face, shortcut);
			creations.put(shortcut, face);
			newFace = null;
		} else {
			mBrowser.addJavascriptInterface(newFace, shortcut);
			creations.put(shortcut, newFace);
		}
		return true;
	}
	public int sizeOfCreations() {
		return creations.size();
	}
	public String getCreations() {
		Set<String> keys = creations.keySet();
		Iterator<String> iterator = keys.iterator();
		String rtn = "";
		while (iterator.hasNext()) {
			if (!rtn.equals("")) rtn += ",";
			String key = iterator.next();
			rtn += "\"" + key + "\":\"" + creations.get(key).getInterfaceName() + "\"";
		}
		return "{" + rtn + "}";
	}

}
